#include <QApplication>
#include <QWidget>  
#include <QPainter>  
#include <QTimer>  
#include <QMouseEvent>  
#include <QVBoxLayout>  

#pragma comment(lib, "user32.lib")

class CrosshairWidget : public QWidget {  
public:  
    CrosshairWidget(QWidget *parent = nullptr) : QWidget(parent) {  
        setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint | Qt::Tool);  
        setAttribute(Qt::WA_TranslucentBackground); // 背景透明  
        setGeometry(100, 100, 200, 200); // 初始位置和大小  

        // 设置定时器，以便定期更新准星的位置  
        QTimer *timer = new QTimer(this);  
        connect(timer, &QTimer::timeout, this, &CrosshairWidget::updatePosition);  
        timer->start(100); // 每100毫秒更新一次  
    }  

protected:  
    void paintEvent(QPaintEvent *event) override {  
        QPainter painter(this);  
        painter.setRenderHint(QPainter::Antialiasing);  

        // 画十字准星  
        painter.setPen(QPen(Qt::red, 2)); // 红色的线条  
        int centerX = width() / 2;  
        int centerY = height() / 2;  
        painter.drawLine(centerX - 10, centerY, centerX + 10, centerY); // 水平线  
        painter.drawLine(centerX, centerY - 10, centerX, centerY + 10); // 垂直线  
    }  

    void mouseMoveEvent(QMouseEvent *event) override {  
        // 更新窗口位置为鼠标当前位置  
        move(event->globalPos() - QPoint(width() / 2, height() / 2));  
    }  

private:  
    void updatePosition() {  
        // 更新位置以确保准星在鼠标附近  
        if (QApplication::mouseButtons() == Qt::LeftButton) {  
            move(QCursor::pos() - QPoint(width() / 2, height() / 2));  
        }  
        update(); // 刷新界面  
    }  
};  

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);  

    CrosshairWidget crosshair;  
    crosshair.show();  

    return app.exec();  
}